'''
水汽： FY2G_2019_08_22_13_00_L_PJ1_W
可见光： FY2G_2019_08_22_13_00_L_PJ1_VIS
红外线： FY2G_2019_08_22_13_00_L_PJ1_IR1
3D: FY2G_2019_08_22_13_00_L_PJ1_3D

http://www.avt7.com/CloudChart/CloudChart?charttype=WVP&chartname=FY2G_2019_08_22_13_00_L_PJ1_W.JPG
http://www.avt7.com/CloudChart/CloudChart?charttype=VIS&chartname=FY2G_2019_08_22_13_00_L_PJ1_VIS.JPG
http://www.avt7.com/CloudChart/CloudChart?charttype=IR1&chartname=FY2G_2019_08_22_13_00_L_PJ1_IR1.JPG
http://www.avt7.com/CloudChart/CloudChart?charttype=3DS&chartname=FY2G_2019_08_22_13_00_L_PJ1_3D.JPG
'''

import os
import time

def check_dir(dir_path):
    '''
    1. 作用:判断文件夹路径是否存在,不存在则创建
    2. 参数:dir_path:文件夹路径
    3. 返回值:文件夹路径
    '''
    if not os.path.exists(dir_path):
        os.makedirs(dir_path)
    return dir_path

base_url_left = 'http://www.avt7.com/CloudChart/CloudChart?charttype='
base_url_right = "{}&chartname=FY2G_{}_{}_{}_{}_00_L_PJ1_{}.JPG"


type_dict  = {'WVP':"W",'VIS':'VIS','IR1':"IR1",'3DS':"3D"}
year_list = ['2016','2017','2018','2019']
month_list = ['01','02','03','04','05','06','07','08','09','10','11','12']
t_list = ['00','01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23']
w_url  = ''

def day_deal(year,month):
    days = lambda year, month: list(range(1, 1+time.localtime(time.mktime((year,month+1,1,0,0,0,0,0,0)) - 86400).tm_mday))
    days_ll = []
    for i in days(year,month):
        t = str(i)
        if len(t) ==1:
            t = t.rjust(2,'0')
        days_ll.append(t)
    return days_ll



def url_deal(file):
    for year in year_list:
        for type_key in type_dict:

            dd = check_dir(file.format(year,type_key))

            for month in month_list:
                days_list = day_deal(int(year),int(month))
                for day in days_list:
                    for t in t_list:
                        bas = base_url_right.format(type_key,year,month,day,t,type_dict[type_key])
                        image_url = base_url_left+bas
                        with open(dd+"/{}.txt".format(month),'a+') as f:
                            f.write(image_url+'\n')
                        print(dd+"/{}.txt".format(month))
                        print(image_url)

if __name__ == '__main__':
    file = '/home/em/桌面/cloud-chart/image' + '/{}'+"/{}"
    url_deal(file)

